
package com.abcijkxyz.dialects.stsm.score;

import org.thymeleaf.IEngineConfiguration;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.engine.AttributeName;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractAttributeTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.standard.expression.IStandardExpression;
import org.thymeleaf.standard.expression.IStandardExpressionParser;
import org.thymeleaf.standard.expression.StandardExpressions;
import org.thymeleaf.templatemode.TemplateMode;

import com.abcijkxyz.domain.extrathyme.Remark;
import com.abcijkxyz.util.RemarkUtil;

/**
 * 获得行的下标、操作行样式
 * 
 * @author root
 *
 */
public class ClassForPositionAttributeTagProcessor extends AbstractAttributeTagProcessor {

	private static final String	ATTR_NAME	= "classforposition";
	private static final int	PRECEDENCE	= 10000;

	public ClassForPositionAttributeTagProcessor(final String dialectPrefix) {
		super(TemplateMode.HTML, // This processor will apply only to HTML mode
				dialectPrefix, // Prefix to be applied to name for matching
				null, // No tag name: match any tag name
				false, // No prefix to be applied to tag name
				ATTR_NAME, // Name of the attribute that will be matched
				true, // Apply dialect prefix to attribute name
				PRECEDENCE, // Precedence (inside dialect's own precedence)
				true); // Remove the matched attribute afterwards
	}

	@Override
	protected void doProcess(final ITemplateContext context, final IProcessableElementTag tag, final AttributeName attributeName, final String attributeValue, final IElementTagStructureHandler structureHandler) {

		final IEngineConfiguration configuration = context.getConfiguration();

		/*
		 * Obtain the Thymeleaf Standard Expression parser
		 */
		final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);

		/*
		 * Parse the attribute value as a Thymeleaf Standard Expression
		 */
		final IStandardExpression expression = parser.parseExpression(context, attributeValue);

		/*
		 * Execute the expression just parsed
		 */
		final Integer position = (Integer) expression.execute(context);

		/*
		 * Obtain the remark corresponding to this position in the league table.
		 */
		final Remark remark = RemarkUtil.getRemarkForPosition(position);

		/*
		 * Select the adequate CSS class for the element.
		 */
		final String newValue;
		if (remark == Remark.WORLD_CHAMPIONS_LEAGUE) {
			newValue = "wcl";
		} else if (remark == Remark.CONTINENTAL_PLAYOFFS) {
			newValue = "cpo";
		} else if (remark == Remark.RELEGATION) {
			newValue = "rel";
		} else {
			newValue = null;
		}

		/*
		 * Set the new value into the 'class' attribute (maybe appending to an existing value)
		 */
		if (newValue != null) {
			if (attributeValue != null) {
				structureHandler.setAttribute("class", attributeValue + " " + newValue);
			} else {
				structureHandler.setAttribute("class", newValue);
			}
		}

	}

}
